package cn.com.zhangwoo.microstar.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import cn.com.microstar.activity.R;
import cn.com.zhangwoo.microstar.fragment.HomeFragment;
import cn.com.zhangwoo.microstar.fragment.MyFragment;
import cn.com.zhangwoo.microstar.fragment.NearbyFragment;
import cn.com.zhangwoo.microstar.fragment.ShoppingCartFragment;
import cn.com.zhangwoo.microstar.img.dao.DensityUtil;
import cn.com.zhangwoo.microstar.utildb.AppManager;

///https://github.com/xy113/XingWeiForAndroid
public class MainActivity extends FragmentActivity {

	/**
	 * 退出时间
	 */
	private long mExitTime;
	/**
	 * 退出间隔
	 */
	private static final int INTERVAL = 2000;
	private static final int[] RADIO_IDS = new int[] { R.id.tab_life,
			R.id.tab_profit, R.id.tab_invitation, R.id.tab_personal };
	private static final int[] TAB_ICON_NORMAL_IDS = new int[] {
			R.drawable.profit_normal, R.drawable.profit_normal,
			R.drawable.invitation_normal, R.drawable.profit_normal };

	private static final int[] TAB_ICON_ACTIVE_IDS = new int[] {
			R.drawable.profit_active, R.drawable.profit_active,
			R.drawable.invitation_active, R.drawable.profit_active };

	private FragmentManager mFragmentManager;
	private HomeFragment f1;
	private ShoppingCartFragment f2;
	private NearbyFragment f3;
	private MyFragment f4;
	private RadioGroup mRadioGroup;
	private int mBackKeyPressedCount;
	private int mBottomDrawableSize;
	private int itemIndex = 0;
	private RadioButton btn1, btn2, btn3, btn4;

	 @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getInstance().addActivity(MainActivity.this);
		setContentView(R.layout.activity_main);
		mRadioGroup = (RadioGroup) findViewById(R.id.bottom_tab_group);
		mFragmentManager = getSupportFragmentManager();
		mBottomDrawableSize = DensityUtil.dip2px(this, 30);
		mRadioGroup.setOnCheckedChangeListener(mOnCheckedChangeListener);
		btn1 = (RadioButton) findViewById(R.id.tab_life);
		btn2 = (RadioButton) findViewById(R.id.tab_profit);
		btn3 = (RadioButton) findViewById(R.id.tab_invitation);
		btn4 = (RadioButton) findViewById(R.id.tab_personal);
		btn1.setChecked(true);
		showFragment(0);
		setBottomTextColor(0);
	}

	private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {

			switch (checkedId) {
			case R.id.tab_life:
				showFragment(0);
				setBottomTextColor(0);
				break;
			case R.id.tab_profit:
				showFragment(1);
				setBottomTextColor(1);
				break;
			case R.id.tab_invitation:
				showFragment(2);
				setBottomTextColor(2);
				break;
			case R.id.tab_personal:
				showFragment(3);
				setBottomTextColor(3);
				break;

			default:
				break;
			}
		}
	};
	@Override
	protected void onActivityResult(int arg0, int arg1,
			android.content.Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 1 && arg1 == 2) {
			showFragment(3);
			setBottomTextColor(3);
		}

	};

	private void showFragment(int page) {
		FragmentTransaction mFragmentTransaction = mFragmentManager
				.beginTransaction();
		if (f1 != null)
			mFragmentTransaction.hide(f1);
		if (f2 != null)
			mFragmentTransaction.hide(f2);
		if (f3 != null)
			mFragmentTransaction.hide(f3);
		if (f4 != null)
			mFragmentTransaction.hide(f4);
		switch (page) {
		case 0:// 首页
			if (f1 == null) {
				f1 = new HomeFragment();
				mFragmentTransaction.add(R.id.main_content, f1);
			} else {
				mFragmentTransaction.show(f1);
			}
			break;
		case 1:// 杂志

			if (f2 != null) {
				mFragmentTransaction.remove(f2);
			}
			f2 = new ShoppingCartFragment();
			mFragmentTransaction.add(R.id.main_content, f2);
			break;
		case 2:// 测评
			if (f3 == null) {
				f3 = new NearbyFragment();// ShoppingCartFragment();
				mFragmentTransaction.add(R.id.main_content, f3);
			} else {
				mFragmentTransaction.show(f3);
			}
			break;
		case 3:// 论坛
			if (f4 == null) {
				f4 = new MyFragment();
				mFragmentTransaction.add(R.id.main_content, f4);
			} else {
				mFragmentTransaction.show(f4);
			}
			break;
		default:
			break;
		}
		mFragmentTransaction.commitAllowingStateLoss();

	}

	private void setBottomTextColor(int position) {
		btn1.setTextColor(getResources().getColor(R.color.tab_text_normal));
		btn2.setTextColor(getResources().getColor(R.color.tab_text_normal));
		btn3.setTextColor(getResources().getColor(R.color.tab_text_normal));
		btn4.setTextColor(getResources().getColor(R.color.tab_text_normal));
		switch (position) {
		case 0:
			btn1.setTextColor(getResources().getColor(R.color.tab_text_selected));
			break;
		case 1:
			btn2.setTextColor(getResources().getColor(R.color.tab_text_selected));
			break;
		case 2:
			btn3.setTextColor(getResources().getColor(R.color.tab_text_selected));
			break;
		case 3:
			btn4.setTextColor(getResources().getColor(R.color.tab_text_selected));
			break;
		default:
			break;
		}
		
	}

	/**
	 * 判断两次返回时间间隔,小于两秒则退出程序
	 */
	private void exit() {
		if (System.currentTimeMillis() - mExitTime > INTERVAL) {
			Toast.makeText(this, "再按一次退出星微", 1000).show();
			mExitTime = System.currentTimeMillis();
		} else {
			AppManager.getInstance().AppExit(getApplicationContext());
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (event.getAction() == KeyEvent.ACTION_DOWN
					&& event.getRepeatCount() == 0) {
				exit();
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}
}
